package org.edu.modules.stu.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.edu.common.api.vo.Result;
import org.edu.modules.stu.base.dto.VeBaseStudentDto;
import org.edu.modules.stu.base.service.VeBaseStudentService;
import org.edu.modules.stu.common.entity.*;
import org.edu.modules.stu.common.service.*;
import org.edu.modules.stu.stu.entity.StuCheck;
import org.edu.modules.stu.stu.entity.VeStuCheck;
import org.edu.modules.stu.stu.entity.VeStuCheckType;
import org.edu.modules.stu.stu.mapper.StuCheckMapper;
import org.edu.modules.stu.stu.mapper.VeStuCheckMapper;
import org.edu.modules.stu.stu.service.StuCheckService;
import org.edu.modules.stu.stu.service.VeStuCheckTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StuCheckServiceImpl extends ServiceImpl<StuCheckMapper, StuCheck> implements StuCheckService {

    @Autowired
    private StuCheckMapper stuCheckMapper;

    @Autowired
    private VeStuCheckMapper veStuCheckMapper;


    @Autowired
    private VeBaseStudentService veBaseStudentService;

    @Autowired
    private VeBaseFacultyService veBaseFacultyService;


    @Autowired
    private VeStuCheckTypeService veStuCheckTypeService;

    @Autowired
    private VeBaseBanjiService veBaseBanjiService;

    @Autowired
    private VeBaseSpecialtyService veBaseSpecialtyService;

    @Autowired
    private VeBaseSemesterService veBaseSemesterService;

    @Autowired
    private VeBaseCalendarService veBaseCalendarService;


    @Override
    public List<Map<String, String>> selectStuChecklist(StuCheck stuCheck) {
        return stuCheckMapper.selectStuChecklist(stuCheck);
    }

    public VeBaseStudentDto getVeBaseStudentDto(String stuId) {
        return veBaseStudentService.findDtoById(stuId);
    }

    public VeBaseStudentDto getVeBaseStudentByXh(String code) {
        return veBaseStudentService.findDtoByXh(code);
    }

    public int getVeBaseFaculty(String name) {
        List<VeBaseFaculty> list = veBaseFacultyService.findAll();
        for (VeBaseFaculty veBaseFaculty : list) {
            if (veBaseFaculty.getYxmc().equals(name)) {
                return veBaseFaculty.getId();
            }
        }
        return 0;
    }

    public int getveStuCheckType(String name) {
        List<VeStuCheckType> list = veStuCheckTypeService.findAll();
        for (VeStuCheckType veStuCheckType : list) {
            if (veStuCheckType.getTitle().equals(name)) {
                return veStuCheckType.getId();
            }
        }
        return 0;
    }

    public int getVeBaseBanji(String name) {
        List<VeBaseBanji> list = veBaseBanjiService.findAll();
        for (VeBaseBanji veBaseBanji : list) {
            if (veBaseBanji.getXzbmc().equals(name)) {
                return veBaseBanji.getId();
            }
        }
        return 0;
    }

    public int getNianji(String name) {
        List<VeBaseBanji> list = veBaseBanjiService.findAll();
        for (VeBaseBanji veBaseBanji : list) {
            if (veBaseBanji.getNjmc().equals(name)) {
                return veBaseBanji.getGradeId();
            }
        }
        return 0;
    }

    public int getzhuangye(String name) {
        List<VeBaseSpecialty> list = veBaseSpecialtyService.findAll();
        for (VeBaseSpecialty veBaseSpecialty : list) {
            if (veBaseSpecialty.getZymc().equals(name)) {
                return veBaseSpecialty.getId();
            }
        }
        return 0;
    }

    public int getSemester(String name) {
        List<VeBaseSemester> list = veBaseSemesterService.findAll();
        for (VeBaseSemester veBaseSemester : list) {
            if (veBaseSemester.getXqmc().equals(name)) {
                return veBaseSemester.getId();
            }
        }
        return 0;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> importExcel(String filePath) {
        try (FileInputStream fis = new FileInputStream(new File(filePath));
             Workbook workbook = new XSSFWorkbook(fis)) {
            Sheet sheet = workbook.getSheetAt(0);
            int rowCount = sheet.getLastRowNum();
            List<VeStuCheck> stuCheckList = new ArrayList<>();
            for (int i = 1; i < rowCount; i++) {
                Row row = sheet.getRow(i);
                VeStuCheck stuCheck = new VeStuCheck();

                //查学生
                //stuCheck.setStuId((int) row.getCell(0).getNumericCellValue());
                stuCheck.setName(row.getCell(1).getStringCellValue());
                stuCheck.setCode(row.getCell(2).getStringCellValue());
                //根据学号查学生
                VeBaseStudentDto veBaseStudentByXh = getVeBaseStudentByXh(row.getCell(2).getStringCellValue());
                stuCheck.setStuId(veBaseStudentByXh.getId());

                stuCheck.setFalId(getVeBaseFaculty(row.getCell(3).getStringCellValue()));//学院
                stuCheck.setSpecId(getzhuangye(row.getCell(4).getStringCellValue()));//专业
                int nianjiid = getNianji(row.getCell(5).getStringCellValue());

                stuCheck.setGradeId(nianjiid);//年级
                stuCheck.setBjId(getVeBaseBanji(row.getCell(6).getStringCellValue()));
//                stuCheck.setJxbjId((int) row.getCell(7).getNumericCellValue());
                stuCheck.setTypeId(getveStuCheckType(row.getCell(7).getStringCellValue()));
                //学期名称
                String xqmc = row.getCell(8).getStringCellValue();
                int semId = getSemester(xqmc);
                stuCheck.setSemId(semId);
                stuCheck.setContent(row.getCell(9).getStringCellValue());
                //日期
                Date date = row.getCell(10).getDateCellValue();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String dates = sdf.format(date);
                //根据学期和日期查校历
                VeBaseCalendar calendar = veBaseCalendarService.getCalendarBySemIdAndDates(semId, dates);
                stuCheck.setCalendarId(calendar.getId());
                //异常时间
                stuCheck.setCheckTime(date);
                stuCheck.setSection(String.valueOf((int) (row.getCell(11).getNumericCellValue())));
                LambdaQueryWrapper<VeStuCheck> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(VeStuCheck::getSemId, stuCheck.getSemId());
                queryWrapper.eq(VeStuCheck::getCalendarId, stuCheck.getCalendarId());
                queryWrapper.eq(VeStuCheck::getCode, stuCheck.getCode());
                queryWrapper.eq(VeStuCheck::getBjId, stuCheck.getBjId());
                queryWrapper.eq(VeStuCheck::getSection, stuCheck.getSection());
                List<VeStuCheck> checkList = veStuCheckMapper.selectList(queryWrapper);
                if (checkList != null && checkList.size() > 0) {
                    //如果有值，说明该学生，该学期的该该节课已经有异常了，无需重复添加
                    return Result.error("学号:" + stuCheck.getCode() + "的学生，在" + dates + "时间的第" + stuCheck.getSection() + "节课已经有异常考勤，请勿重复添加");
                }
                stuCheckList.add(stuCheck);
            }
            for (VeStuCheck check : stuCheckList) {
                veStuCheckMapper.insert(check);
            }
            return Result.OK("数据导入成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.error("数据导入失败");
    }
}
